package com.newcoin.chat.controller;

import com.newcoin.chat.beans.entity.GroupApplicationEntity;
import com.newcoin.chat.beans.vo.*;
import com.newcoin.chat.service.GroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/group")
@Slf4j
public class GroupController {
    @Autowired
    private GroupService service;

    /**
     * 创建群聊
     *
     * @param userId
     * @param userIds
     * @param sign
     * @return
     */
    @PostMapping(value = "/createGroup")
    public ResultVo<Object> createGroup(long userId, String userIds, String sign) {
        try {
            return service.createGroup(userId, userIds, sign);
        } catch (Exception e) {
            ResultVo<Object> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("服务器出错");
            return resultVo;
        }
    }

    /**
     * 修改群名称
     *
     * @param userId
     * @param groupId
     * @param name
     * @return
     */
    @PostMapping(value = "/modifyGroupName")
    public ResultVo modifyGroupName(long userId, long groupId, String name) {
        try {
            return service.modifyGroupName(userId, groupId, name);
        } catch (Exception e) {
            ResultVo<Object> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("服务器出错");
            return resultVo;
        }
    }

    /**
     * 是否有新的群邀请信息
     *
     * @param userId
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/isThereANewGroupApplication")
    public ResultVo<Boolean> isThereANewGroupApplication(@RequestParam long userId) {
        try {
            return service.isThereANewGroupApplication(userId);
        } catch (Exception e) {
            ResultVo<Boolean> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("服务器出错");
            return resultVo;
        }
    }

    /**
     * 获取最近10条群邀请消息
     *
     * @param userId
     * @return
     */
    @PostMapping(value = "/groupInviteMessages")
    public ResultVo<List<GroupApplicationEntity>> groupInviteMessages(long userId) {
        try {
            return service.groupInviteMessages(userId);
        } catch (Exception e) {
            ResultVo<List<GroupApplicationEntity>> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("服务器出错");
            return resultVo;
        }
    }

    /**
     * 获取若有群邀请信息
     *
     * @param userId
     * @param page
     * @return
     */
    @PostMapping(value = "/getAllGroupApplicationMessages")
    public ResultVo<GroupApplicationVo> getAllGroupApplicationMessages(long userId, int page) {
        try {
            return service.getAllGroupApplicationMessages(userId, page);
        } catch (Exception e) {
            log.error(e.getMessage());
            ResultVo<GroupApplicationVo> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("服务器出错");
            return resultVo;
        }
    }

    /**
     * 同意或者拒绝群邀请
     *
     * @param groupApplicationId
     * @param state
     * @return
     */
    @PostMapping(value = "/passOrRejectGroupApplication")
    public ResultVo<String> passOrRejectGroupApplication(long groupApplicationId, int state) {
        try {
            return service.passOrRejectGroupApplication(groupApplicationId, state);
        } catch (Exception e) {
            ResultVo<String> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("服务器出错");
            return resultVo;
        }
    }

    /**
     * 移除群成员
     *
     * @param userId
     * @param groupId
     * @param groupMemberId
     * @return
     */
    @PostMapping(value = "/removeUser")
    public ResultVo removeUser(long userId, long groupId, long groupMemberId) {
        try {
            return service.removeUser(userId, groupId, groupMemberId);
        } catch (Exception e) {
            e.printStackTrace();
            ResultVo resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("服务器出错");
            return resultVo;
        }
    }

    /**
     * 群成员退出群聊
     *
     * @param userId
     * @param groupId
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/dropOutGroup")
    public ResultVo dropOutGroup(long userId, long groupId) {
        try {
            return service.dropOutGroup(userId, groupId);
        } catch (Exception e) {
            e.printStackTrace();
            ResultVo resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("服务器出错");
            return resultVo;
        }
    }

    /**
     * 邀请好友进群（可以是群主  也可以是群成员）
     *
     * @param userId
     * @param friendId
     * @param groupId
     * @return
     */
    @PostMapping(value = "/inviteFriendAddGroup")
    public ResultVo inviteFriendAddGroup(long userId, long friendId, long groupId) {
        try {
            return service.inviteFriendAddGroup(userId, friendId, groupId);
        } catch (Exception e) {
            e.printStackTrace();
            ResultVo resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("服务器出错");
            return resultVo;
        }
    }

    /**
     * 群主打开或关闭阅后即焚
     *
     * @param userId
     * @param state
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/openOrCloseAfterReadInc")
    public ResultVo openOrCloseAfterReadingIncineration(long userId, long groupId, int state) {
        try {
            return service.openOrCloseAfterReadingIncineration(userId, groupId, state);
        } catch (Exception e) {
            log.error(e.getMessage());
            ResultVo resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("服务器出错");
            return resultVo;
        }
    }

    /**
     * 根据群id获取群信息
     *
     * @param groupId
     * @return
     */
    @GetMapping(value = "/getGroupInfoByGroupId")
    public ResultVo<GroupInfoVo> getGroupInfoByGroupId(@RequestParam long groupId) {
        try {
            return service.getGroupInfoByGroupId(groupId);
        } catch (Exception e) {
            ResultVo<GroupInfoVo> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("系统出错");
            return resultVo;
        }
    }

    /**
     * 获取群成员
     *
     * @param groupId
     * @return
     */
    @GetMapping(value = "/getGroupMember")
    public ResultVo<List<UserVo>> getGroupMember(long groupId) {
        try {
            return service.getGroupMember(groupId);
        } catch (Exception e) {
            ResultVo<List<UserVo>> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("系统出错");
            return resultVo;
        }
    }

    /**
     * 获取群群阅后即焚的状态
     *
     * @param groupId
     * @return
     */
    @GetMapping(value = "/getAfterReadingIncinerationState")
    public ResultVo<Integer> getAfterReadingIncinerationState(long groupId) {
        try {
            return service.getAfterReadingIncinerationState(groupId);
        } catch (Exception e) {
            ResultVo<Integer> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("系统出错");
            return resultVo;
        }
    }

    /**
     * 获取群离线消息
     *
     * @param userId
     * @param groupId
     * @return
     */
    @GetMapping(value = "/getOfflineMessage")
    public ResultVo<List<GroupOfflineMsgVo>> getOfflineMessage(Long userId, Long groupId) {
        try {
            return service.getOfflineMessage(userId, groupId);
        } catch (Exception e) {
            e.printStackTrace();
            ResultVo<List<GroupOfflineMsgVo>> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("系统出错");
            return resultVo;
        }
    }

    /**
     * 修改群名称
     * @param groupId
     * @param groupName
     * @return
     */
    @PostMapping(value = "/updateGroupName")
    public ResultVo updateGroupName(Long groupId, String groupName) {
        try {
            return service.updateGroupName(groupId, groupName);
        } catch (Exception e) {
            e.printStackTrace();
            ResultVo<List<GroupOfflineMsgVo>> resultVo = new ResultVo<>();
            resultVo.setStatus(500);
            resultVo.setMsg("系统出错");
            return resultVo;
        }
    }

}
